package com.timber.panels;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import pv.jfcx.JPVCurrency;
import pv.jfcx.JPVEdit;

import com.borland.jbcl.layout.VerticalFlowLayout;
import com.timber.frame.ImsFrame;
import com.timber.util.ITPEdit;
import com.timber.util.ImageMgr;
import com.timber.util.InvoiceMgr;
import com.timber.util.IteamBean;
import com.timber.util.TimberdetailMgr;

public class BilliAdjPanel extends JPanel{
	protected ImsFrame parent;
	protected GridBagLayout gridBagLayout1 = new GridBagLayout();
	protected GridBagLayout gridBagLayout2 = new GridBagLayout();
	protected JPVCurrency LengthTF = new JPVCurrency();
	protected JLabel LengthLabel; 
	protected TitledBorder		titledBorder;
	protected ITPEdit billNumTF = new ITPEdit();
	protected ITPEdit ItemNameTF = new ITPEdit();
	protected ITPEdit ItemClassTF = new ITPEdit();
	protected JLabel billNumLabel; 
	protected JLabel			timberNameLabel = new JLabel();
	protected JLabel			classLabel = new JLabel();
	protected JPVCurrency taxTF = new JPVCurrency();
	protected JLabel taxLabel; 
	protected JPVCurrency widthTF = new JPVCurrency();
	protected JLabel 		widthLabel;
	protected JPVCurrency qtyTF = new JPVCurrency();
	protected JLabel 		qtyLabel;
	protected JPVCurrency rateTF = new JPVCurrency();
	protected JLabel 		rateLabel;
	protected JPVCurrency totalTF = new JPVCurrency();
	protected JLabel 	totalLabel;
	protected JPVCurrency GrandTotalTF = new JPVCurrency();
	protected JLabel GrandTotalLabel; 
	protected JPanel			woodPanel = new JPanel();
	protected JPanel			topPanel = new JPanel();
	protected JPanel			topPanel2 = new JPanel();
	protected JPanel			topPanel3 = new JPanel();
	protected VerticalFlowLayout		verticalFlowLayout1 = new VerticalFlowLayout();
	protected GridBagLayout	 gridBagLayout3=new GridBagLayout();
	protected Component			hstrut =  Box.createRigidArea(new Dimension(280,20));
	protected JButton			addBtn = new JButton();
	protected ImageIcon			addImage;
	TimberdetailMgr objTimberdetailMgr=new TimberdetailMgr();
	Vector currentInvoice= new Vector();
	protected Color					focusBG;
	protected Color					nonFocusBG;
	
	public BilliAdjPanel(ImsFrame parent, boolean showExtraButtons) {
		this.parent = parent;
		
		try {
			jbInit();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	void jbInit() throws Exception {
		
		this.setLayout(verticalFlowLayout1);
		this.setBackground(new Color(204, 204, 204));
		this.setMinimumSize(new Dimension(650, 197));    
		this.setPreferredSize(new Dimension(650, 150)); 
		Dimension d = new Dimension(80,20);
		titledBorder = new TitledBorder( BorderFactory.createEtchedBorder(), " Qty" );
		
		addImage       = ImageMgr.getIcon( ImageMgr.PLUS_ICON );
		topPanel.setLayout( gridBagLayout1 );
		topPanel2.setLayout( gridBagLayout3 );
		woodPanel.setLayout( gridBagLayout2 );
		
		topPanel3.setBorder( titledBorder );
		billNumLabel  = new JLabel("Bill No");
		LengthLabel = new JLabel("Length");
		widthLabel = new JLabel("Width"); 
		taxLabel  = new JLabel("Tax");
		qtyLabel=new JLabel("Cubic Qty");
		rateLabel=new JLabel("Rate");
		totalLabel=new JLabel("Total");
		GrandTotalLabel=new JLabel("GrandTotal");
		classLabel.setText( "Timber Type" );
		timberNameLabel.setText( "Timber Name" );
		
		billNumTF.setMaxLength( 9 );
		billNumTF.setCase( JPVEdit.UPPER );
		billNumTF.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		ItemNameTF.setMaxLength( 9 );
		ItemNameTF.setCase( JPVEdit.UPPER );
		ItemNameTF.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		ItemClassTF.setMaxLength( 9 );
		ItemClassTF.setCase( JPVEdit.UPPER );
		ItemClassTF.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		
		LengthTF.setDecimals(4);
		LengthTF.setSymbol("");
		LengthTF.setText("");        
		LengthTF.setPreferredSize(d);
		LengthTF.setMinimumSize(d);
		LengthTF.setAlignment(2);

		LengthTF.addFocusListener(new java.awt.event.FocusAdapter() {
		
			public void focusLost(FocusEvent e) {
				
				calQty();
				calTotalandTax();
				
			}
		});
		
		
		widthTF.setDecimals(4);
		widthTF.setSymbol("");
		widthTF.setText("");        
		widthTF.setPreferredSize(d);
		widthTF.setMinimumSize(d);
		widthTF.setAlignment(2);
		
		widthTF.addFocusListener(new java.awt.event.FocusAdapter() {
			
			public void focusLost(FocusEvent e) {
				
				calQty();
				calTotalandTax();
				
			}
		});
		
		
		
		taxTF.setSymbol("");
		taxTF.setDecimals(4);
		taxTF.setSymbol("");
		taxTF.setText("");        
		taxTF.setPreferredSize(d);
		taxTF.setMinimumSize(d);
		taxTF.setAlignment(2);
		taxTF.setEditable(false);
		
		rateTF.setDecimals(4);
		rateTF.setSymbol("");
		rateTF.setText("");        
		rateTF.setPreferredSize(d);
		rateTF.setMinimumSize(d);
		rateTF.setAlignment(2);
		rateTF.addFocusListener(new java.awt.event.FocusAdapter() {
			
			public void focusLost(FocusEvent e) {
				calTotalandTax();
				
			}
		});
		
		qtyTF.setDecimals(4);
		qtyTF.setSymbol("");
		qtyTF.setText("");        
		qtyTF.setPreferredSize(d);
		qtyTF.setMinimumSize(d);
		qtyTF.setAlignment(2);
		qtyTF.setEditable(false);
		
		
		totalTF.setDecimals(4);
		totalTF.setSymbol("");
		totalTF.setText("");        
		totalTF.setPreferredSize(d);
		totalTF.setMinimumSize(d);
		totalTF.setAlignment(2);
		totalTF.setEditable(false);
		
		addBtn.setIcon( addImage );
		addBtn.setMargin( new Insets( 0, 0, 0, 0 ) );
		addBtn.addActionListener( new ActionListener()
				{
			public void actionPerformed( ActionEvent e )
			{
				System.out.println("Clicked Add Button");
			}
			
			
				} );
		
		// item TF action performed
		
		billNumTF.setMaxLength( 9 );
		billNumTF.setCase( JPVEdit.UPPER );
		billNumTF.setIncludeCharacters( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
		billNumTF.addFocusListener(new java.awt.event.FocusAdapter() {
			public void focusGained(FocusEvent e) {
				if (focusBG != null)
				{
					//itemTF.setBackground(focusBG);
					System.out.println("Focuse gained ");
				}
			}
			public void focusLost(FocusEvent e) {
				{
					InvoiceMgr invoiceMgr = new InvoiceMgr();
					currentInvoice=invoiceMgr.getInviceDetails(billNumTF.getText());
					if(currentInvoice.size()>0)
					{
						
					}
					
				}
			}
		});
		
		topPanel3.setLayout(verticalFlowLayout1);
		this.add(topPanel3, null);
		
		topPanel.add( billNumLabel,
				new GridBagConstraints( 2,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 5, 0, 0 ), 0, 0 ) );
		topPanel.add( billNumTF,
				new GridBagConstraints( 2,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		
		topPanel.add( timberNameLabel,
				new GridBagConstraints( 3,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 5, 0, 0 ), 0, 0 ) );
		topPanel.add( ItemNameTF,
				new GridBagConstraints( 3,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		topPanel.add( classLabel,
				new GridBagConstraints( 4,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		topPanel.add( ItemClassTF,
				new GridBagConstraints( 4,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		
		
		//TODO
		
		topPanel.add( LengthLabel,
				new GridBagConstraints( 5,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		topPanel.add( LengthTF,
				new GridBagConstraints( 5,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		
		
		topPanel.add( widthLabel,
				new GridBagConstraints( 6,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		topPanel.add( widthTF,
				new GridBagConstraints( 6,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		
		
		topPanel.add( qtyLabel,
				new GridBagConstraints( 7,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		topPanel.add( qtyTF,
				new GridBagConstraints(7 ,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 5, 0, 0 ), 0, 0 ) );
		
		
		
		
		topPanel2.add( hstrut,
				new GridBagConstraints( 1,
						0,
						3,
						2,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 10, 15, 0, 0 ), 0, 0 ) );
		
		topPanel2.add( rateLabel,
				new GridBagConstraints( 4,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( rateTF,
				new GridBagConstraints( 4,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		topPanel2.add( taxLabel,
				new GridBagConstraints( 5,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( taxTF,
				new GridBagConstraints( 5,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		topPanel2.add( totalLabel,
				new GridBagConstraints( 6,
						0,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.SOUTHWEST,
						GridBagConstraints.NONE,
						new Insets( 12, 10, 0, 0 ), 0, 0 ) );
		topPanel2.add( totalTF,
				new GridBagConstraints(6,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		topPanel3.add( topPanel,
				new GridBagConstraints(7,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		topPanel3.add( topPanel2,
				new GridBagConstraints(7,
						1,
						1,
						1,
						0.0,
						0.0,
						GridBagConstraints.NORTHWEST,
						GridBagConstraints.NONE,
						new Insets( 0, 10, 0, 0 ), 0, 0 ) );
		
		
		
		
	}
	
	
	public BilliAdjPanel(ImsFrame parent) {
		this.parent = parent;
		try {
			jbInit();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	 public double calQty()
	 {
		 double qty=0;
		// constant for converting to cubic , it should read from property file 
		 double cubiconstant=2;
		 qty=LengthTF.getDouble()*widthTF.getDouble()*cubiconstant;	 
		 qtyTF.setDouble(qty);
		 return qty;
	 }
	 
	 
	 
	 public double calTotalandTax()
	 {
		 double qty=0;
		 double toatl=0;
		 // should get from property file
		 double taxconsant=0.12;
		 qty=calQty();
		
		 // total calculation for differn for TIMBER and WOODPICE
		 
		 toatl=qty*rateTF.getDouble();
		 totalTF.setDouble(toatl);
		 taxTF.setDouble(toatl*taxconsant);
		 return toatl;
	 }
	 
	 
	 public double calamount()
	 {
		 double amount=0;
		 double toatl=0;
		 // should get from property file
		 double taxconsant=0.12;
		 toatl=calTotalandTax();
		 amount=toatl+(toatl*taxconsant);
		 System.out.println("Total  :"+toatl);
		 System.out.println("Tax :"+toatl*taxconsant);
		 System.out.println("amount ---------:"+amount);
		 return amount;
	 }
	 
	 public void clear()
	 {
		 billNumTF.setText("");
		 ItemNameTF.setText(""); 
		 ItemClassTF.setText("");
		 taxTF.setText("");  
		 widthTF.setText("");
		 qtyTF.setText("");
		 rateTF.setText(""); 
		 totalTF.setText("");
		 LengthTF.setText("");
	 }
	 

	
}
